<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Mockery\Exception;
use DB;

/**
 * App\Doctor
 *
 * @mixin \Eloquent
 */
class Doctor extends Model
{
    public $timestamps = false;
    protected $table = 'jkbl_doctor';

    const DEL_OFF = 0;
    const DEL_ON = 1;

    //性别
    const SEX_WOMEN = 0;
    const SEX_MAN = 1;

    static $SexDisplay = [
        self::SEX_WOMEN => '女',
        self::SEX_MAN => '男',
    ];

    //审核状态
    const STATUS_WAITING = 0;
    const STATUS_YES = 1;
    const STATUS_REFUSE = 2;
    const STATUS_NEW = 3;


    static $StatusDisplay = [
        self::STATUS_WAITING => '待审核',
        self::STATUS_YES => '审核通过',
        self::STATUS_REFUSE => '审核失败',
        self::STATUS_NEW => '新信息审核中',
    ];

    const BUY_TYPE_PIC = 1;
    const BUY_TYPE_VOICE = 2;
    const BUY_TYPE_VIDEO = 3;

    static $BuyTypeDisplay = [
        self::BUY_TYPE_PIC => '图文',
        self::BUY_TYPE_VOICE => '语音',
        self::BUY_TYPE_VIDEO => '视频',
    ];


    /**
     * 更新信息
     * @param $doctor
     * @return array|\Exception
     * @throws \Exception
     */
    static function NewInfoOn($doctor)
    {
        DB::beginTransaction();

        $info = DoctorEdit::select('person_name',
            'id_card',
            'sex',
            'age',
            'type',
            'qualification',
            'picconsult',
            'voiceconsult',
            'videoconsult',
            'expert',
            'introduction',
            'avatar',
            'card_up',
            'card_down',
            'qualificationpic',
            'otherpic')
            ->find($doctor->editid);

        $doctor->person_name = $info->person_name;
        $doctor->id_card = $info->id_card;
        $doctor->sex = $info->sex;
        $doctor->age = $info->age;
        $doctor->type = $info->type;
        $doctor->qualification = $info->qualification;
        $doctor->picconsult = $info->picconsult;
        $doctor->voiceconsult = $info->voiceconsult;
        $doctor->videoconsult = $info->videoconsult;
        $doctor->expert = $info->expert;
        $doctor->introduction = $info->introduction;
        $doctor->avatar = $info->avatar;
        $doctor->card_up = $info->card_up;
        $doctor->qualificationpic = $info->qualificationpic;
        $doctor->otherpic = $info->otherpic;
        $doctor->status = Doctor::STATUS_YES;

        try {
            if (!$doctor->save()) {
                DB::rollBack();

                return error('失败');
            }
        } catch (Exception $exception) {
            DB::rollBack();

            logger($exception);
            return error('错误');
        }

        //发送推送消息
        $Jpush = JpushShMessage::SendJpush($doctor->shid,JpushShMessage::TYPE_SYSTEM,"您修改的{$info->person_name}审核通过。",JpushShMessage::RECEIVE_SH);
        if ($Jpush['status'] === 2) {
            DB::rollBack();

            return $Jpush;
        }

        DB::commit();

        return success('成功');
    }


    /**
     * 医师审核通过或者新信息通过审核
     * @param $doctor
     * @return array|\Exception
     * @throws \Exception
     */
    static function StatusOn($doctor)
    {
        DB::beginTransaction();
        //修改状态
        $doctor->status = Doctor::STATUS_YES;
        if (!$doctor->save()) {
            return error('失败');
        }

        //发送推送消息
        $Jpush = JpushShMessage::SendJpush($doctor->shid,JpushShMessage::TYPE_SYSTEM,"您添加的{$doctor->person_name}审核通过。",JpushShMessage::RECEIVE_SH);
        if ($Jpush['status'] === 2) {
            DB::rollBack();

            return $Jpush;
        }

        DB::commit();

        return success('成功');
    }

    /**
     * 医师不通过审核
     * @param $doctor
     * @return array|\Exception
     * @throws \Exception
     */
    static function StatusOFF($doctor)
    {
        DB::beginTransaction();
        //修改状态
        $doctor->status = Doctor::STATUS_REFUSE;
        if (!$doctor->save()) {
            return error('失败');
        }

        //发送推送消息
        $Jpush = JpushShMessage::SendJpush($doctor->shid,JpushShMessage::TYPE_SYSTEM,"您的{$doctor->person_name}的信息审核未通过。请重新修改。",JpushShMessage::RECEIVE_SH);
        if ($Jpush['status'] === 2) {
            DB::rollBack();

            return $Jpush;
        }

        DB::commit();

        return success('成功');
    }

}
